package cn.tj212.xiaoyin.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BeanUtil {

    private static Logger logger=LoggerFactory.getLogger(BeanUtil.class);

    //将mapList转换为beanList
    public static <T>List<T> mapListToBeanList(Class<T> clazz, List<Map<String,Object>> mapList){
        List<T> rstList=new ArrayList<T>();
        for (Map<String,Object> map:mapList
             ) {
            rstList.add(mapToBean(clazz,map));
        }
        return rstList;
    }

    /**
     * 将map转化为bean
     * @param clazz
     * @param map
     * @param <T>
     * @return
     */
    private static <T> T mapToBean(Class<T> clazz, Map<String,Object> map) {
        if (map==null)
            return null;
        //创建对象
        T instance=null;
        try{
            instance=clazz.newInstance();
            if (instance==null)
                throw new Exception();
        }catch (Exception e){
            logger.error("类型实例化对象失败："+clazz);
            return null;
        }
        Map<String,Object> newMap=new HashMap<String, Object>();
        for (Map.Entry<String,Object> en :map.entrySet()
             ) {
            newMap.put(columnSetMethod(en.getKey().toLowerCase()),en.getValue());
        }
        Method[] methods=clazz.getMethods();
    }

    private static String columnSetMethod(String s) {
    }

    public static String fieldToColumn(String str) {
        char[] chars=str.toCharArray();
        String rstStr="";
        for (int i = 0; i < chars.length; i++) {
            if (chars[i]>64&&chars[i]<94){
                rstStr+=("_"+chars[i]).toLowerCase();
            }else {
                rstStr+=chars[i];

            }
        }
        return rstStr;
    }

    public static <T> String getTableName(Class<T> eneityClass) {
    }

    public static <T> Map<String,String> getAllFieldColumns(Class<?> entityClass) {
    }
}
